package com.baibei.product.merge;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baibei.basic.BasicRecyclerViewAdapter;
import com.baibei.basic.IPresenter;
import com.baibei.basic.SimpleViewHolder;
import com.baibei.model.MergeOrderInfo;
import com.baibei.module.AppRouter;
import com.baibei.module.BasicActivity;
import com.baibei.module.GlideApp;
import com.baibei.product.R;
import com.baibei.product.R2;
import com.baibei.widget.CommonRefreshLayout;
import com.baibei.widget.RaeRecyclerView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.makeramen.roundedimageview.RoundedImageView;
import com.rae.swift.Rx;
import com.rae.widget.RaePlaceHolderLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 拼单
 * Created by ChenRui on 2017/8/15 0015 11:03.
 */
@Route(path = AppRouter.ROUTER_MERGE_ORDERS)
public class MergeOrderActivity extends BasicActivity implements MergeOrderContract.View {

    @BindView(R2.id.rec)
    RaeRecyclerView mRecyclerView;

    MergeOrderAdapter mAdapter;

    @BindView(R2.id.content)
    CommonRefreshLayout mContentLayout;

    @BindView(R2.id.placeholder)
    RaePlaceHolderLayout mPlaceHolderLayout;
    private MergeOrderContract.Presenter mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merge_order);
        mPresenter = inject(MergeOrderContract.Presenter.class);
        mAdapter = new MergeOrderAdapter();
        mPlaceHolderLayout.subscribe(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {

            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                mPresenter.loadMore();
            }
        });
        mContentLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                mPresenter.start();
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return super.checkCanDoRefresh(frame, content, header) && mRecyclerView.isOnTop();
            }
        });
        mPresenter.start();
    }

    @Override
    public IPresenter getBasicPresenter() {
        return mPresenter;
    }


    @Override
    public void onNoMoreData() {
        mContentLayout.refreshComplete();
        mRecyclerView.loadMoreComplete();
        mRecyclerView.setNoMore(true);
    }

    @Override
    public void onEmptyData(String msg) {
        mContentLayout.refreshComplete();
        mPlaceHolderLayout.setText(msg);
        mPlaceHolderLayout.onEmpty();
    }

    @Override
    public void onLoadData(List<MergeOrderInfo> list) {
        mContentLayout.refreshComplete();
        mAdapter.setDataList(list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoginExpired() {

    }

    public class MergeOrderViewHolder extends SimpleViewHolder {

        @BindView(R2.id.img_product)
        RoundedImageView mImgProduct;
        @BindView(R2.id.tv_title)
        TextView mTitle;
        @BindView(R2.id.tv_progress)
        TextView mProgressText;
        @BindView(R2.id.progress)
        ProgressBar mProgress;
        @BindView(R2.id.tv_price)
        TextView mPrice;
        @BindView(R2.id.btn_detail)
        Button mDetailButton;

        public MergeOrderViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class MergeOrderAdapter extends BasicRecyclerViewAdapter<MergeOrderInfo, MergeOrderViewHolder> {

        private View.OnClickListener mDetailClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = (int) view.getTag();
                MergeOrderInfo item = getDataItem(position);
                AppRouter.routeToMergeOrderDetail(view.getContext(), item);

            }
        };

        @Override
        protected void onItemClick(View v, int position, MergeOrderInfo m) {
            super.onItemClick(v, position, m);
            AppRouter.routeToMergeOrderDetail(v.getContext(), m);
        }

        @Override
        protected MergeOrderViewHolder onCreateViewHolder(LayoutInflater layoutInflater, ViewGroup viewGroup, int viewType) {
            return new MergeOrderViewHolder(inflater(R.layout.item_merge_order, viewGroup));
        }

        @Override
        protected void onBindViewHolder(MergeOrderViewHolder holder, int position, MergeOrderInfo m) {
            holder.mTitle.setText(m.getName());
            holder.mProgressText.setText("拼单进度：" + Rx.formatPercent(m.getProgress()));
            holder.mProgress.setProgress((int) (m.getProgress() * 100));
            holder.mPrice.setText(Rx.formatPrice(m.getCrawdPrice()));
            holder.mDetailButton.setTag(position);
            holder.mDetailButton.setOnClickListener(mDetailClickListener);
            GlideApp.with(holder.itemView.getContext())
                    .load(m.getProductPic())
                    .into(holder.mImgProduct);
        }

    }
}
